#include "interestchange.h"
#include "ui_interestchange.h"
#include <QFileDialog>
#include <QDesktopServices>
#include "myclicklabel.h"
#include "QNetworkRequest"
#include "QNetworkReply"
#include "QJsonParseError"
#include "QJsonArray"
#include "QJsonDocument"
#include "QJsonObject"
#include "QJsonValueRef"

interestChange::interestChange(QWidget *parent, Person * m_user) :
    QWidget(parent),
    ui(new Ui::interestChange),
    interest_user(m_user)
{
    ui->setupUi(this);

    basicSettings();
    connectSettings();
}

interestChange::~interestChange()
{
    delete ui;
}

void interestChange::basicSettings()
{
    ui->labColour->setAlignment(Qt::AlignCenter);
    ui->labComic->setAlignment(Qt::AlignCenter);
    ui->labOriginal->setAlignment(Qt::AlignCenter);

    //按钮初始不可点击
    ui->btnComic->setEnabled(false);
    ui->btnComic->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(128, 128, 128);}");
    ui->btnColour->setEnabled(false);
    ui->btnColour->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(128, 128, 128);}");
}

void interestChange::connectSettings()
{
    connect(ui->btnColour, &QPushButton::clicked, this, &interestChange::btnColour_clicked);
    connect(ui->btnComic, &QPushButton::clicked, this, &interestChange::btnComic_clicked);
    connect(ui->btnOriginal, &QPushButton::clicked, this, &interestChange::btnOriginal_clicked);
    connect(ui->btnBeauty, &QPushButton::clicked, this, &interestChange::btnBeauty_clicked);
    connect(ui->labColour, &MyClickLabel::clicked, [=](){
        QDesktopServices::openUrl(QUrl(QString("file:///") + colourPath));
    });
    connect(ui->labComic, &MyClickLabel::clicked, [=](){
        QDesktopServices::openUrl(QUrl(QString("file:///") + comicPath));
    });
}

void interestChange::dealMsg(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QJsonParseError error;
    QJsonDocument data = QJsonDocument::fromJson(array,&error);
    if(!data.isNull())
    {
        QJsonArray obj = data.array();
        QJsonObject object = obj[0].toObject();
        colourPath = object.value("path").toString();

        if(!colourPath.isNull())
        {
            ui->labColour->setPixmap(QPixmap(colourPath).scaled(ui->labColour->width(), ui->labColour->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }
    }
    emit closeWait();
}

void interestChange::btnColour_clicked()
{
    //信号发送
    // URL
    QString baseUrl = "http://127.0.0.1:5000/PhotoColour?Userid=" + interest_user->getId() + "&Path=" + originalPath;
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    emit showWait();

    connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(dealMsg(QNetworkReply*)));
}

void interestChange::dealMsg1(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QJsonParseError error;
    QJsonDocument data = QJsonDocument::fromJson(array,&error);
    if(!data.isNull())
    {
        QJsonArray obj = data.array();
        QJsonObject object = obj[0].toObject();
        comicPath = object.value("path").toString();

        if(!comicPath.isNull())
        {
            ui->labComic->setPixmap(QPixmap(comicPath).scaled(ui->labComic->width(), ui->labComic->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }
    }
    emit closeWait();
}

void interestChange::btnComic_clicked()
{
    //信号发送
    // URL
    QString baseUrl = "http://127.0.0.1:5000/PhotoCartoon?Userid=" + interest_user->getId() + "&Path=" + originalPath;
    QUrl url(baseUrl);
    qDebug() << baseUrl;

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    emit showWait();

    connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(dealMsg1(QNetworkReply*)));
}

void interestChange::btnOriginal_clicked()
{
    originalPath = QFileDialog::getOpenFileName(this, "Open File", "E:\\Python\\FinalAIface\\test", "Image File(*.jpg;*.png;*.jfif)");

    if(!originalPath.isNull())
    {
        ui->labOriginal->setPixmap(QPixmap(originalPath).scaled(ui->labOriginal->width(), ui->labOriginal->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        ui->btnComic->setEnabled(true);
        ui->btnComic->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(255, 0, 0);}"
                                     "QPushButton:hover{background-color:rgb(220, 0, 0);}"
                                     "QPushButton:pressed{background-color:rgb(200, 0, 0);}");
        ui->btnColour->setEnabled(true);
        ui->btnColour->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(255, 0, 0);}"
                                     "QPushButton:hover{background-color:rgb(220, 0, 0);}"
                                     "QPushButton:pressed{background-color:rgb(200, 0, 0);}");
    }
}

void interestChange::btnBeauty_clicked()
{
    // URL
    QString baseUrl = "http://127.0.0.1:5000/FaceBeauty";
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);
}
